﻿using System;
using System.Collections.Generic;
using RibbonLib;
using RibbonLib.Interop;

namespace todo.Ribbon
{
    public class RibbonRecentItems : RibbonControl
    {
        public RibbonRecentItems(RibbonUI ribbonUI, uint commandID) : base(ribbonUI, commandID)
        {
        }

        private IList<RecentItemsPropertySet> _items;

        public IList<RecentItemsPropertySet> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public event Action<RecentItemsPropertySet> OnPinStateChanged;
        public event Action<RecentItemsPropertySet> OnClick;

        public void FireOnClick(RecentItemsPropertySet item)
        {
            var handler = OnClick;
            if (handler != null) handler(item);
        }

        public void FireOnPinStateChanged(RecentItemsPropertySet item)
        {
            var handler = OnPinStateChanged;
            if (handler != null) handler(item);
        }

        public override HRESULT UpdateProperty(ref PropertyKey key, PropVariantRef currentValue,
                                               ref PropVariant newValue)
        {
            if (key == RibbonProperties.RecentItems)
            {
                if (_items != null)
                {
                    var arr = new RecentItemsPropertySet[_items.Count];
                    for (var n = 0; n < arr.Length; n++)
                    {
                        arr[n] = _items[n];
                    }
                    newValue.SetSafeArray(arr);
                }
            }

            return HRESULT.S_OK;
        }

        public override HRESULT Execute(ExecutionVerb verb, PropertyKeyRef key,
                                        PropVariantRef currentValue,
                                        IUISimplePropertySet commandExecutionProperties)
        {
            if (key.PropertyKey == RibbonProperties.RecentItems)
            {
                // go over recent items
                var objectArray = (object[]) currentValue.PropVariant.Value;
                for (var i = 0; i < objectArray.Length; ++i)
                {
                    var propertySet = objectArray[i] as IUISimplePropertySet;

                    if (propertySet != null)
                    {
                        PropVariant propLabel;
                        propertySet.GetValue(ref RibbonProperties.Label,
                                out propLabel);
                        var label = (string) propLabel.Value;

                        PropVariant propLabelDescription;
                        propertySet.GetValue(ref RibbonProperties.LabelDescription,
                                out propLabelDescription);
                        var labelDescription = (string) propLabelDescription.Value;

                        PropVariant propPinned;
                        propertySet.GetValue(ref RibbonProperties.Pinned,
                                out propPinned);
                        var pinned = (bool) propPinned.Value;

                        // update pinned value
                        var item = _items[i];
                        if (item.Pinned != pinned)
                        {
                            item.Pinned = pinned;
                            FireOnPinStateChanged(item);
                        }
                    }
                }
            }
            else if (key.PropertyKey == RibbonProperties.SelectedItem)
            {
                // get selected item index
                var selectedItem = (uint) currentValue.PropVariant.Value;

                // get selected item label
                PropVariant propLabel;
                commandExecutionProperties.GetValue(ref RibbonProperties.Label,
                        out propLabel);
                var label = (string) propLabel.Value;

                // get selected item label description
                PropVariant propLabelDescription;
                commandExecutionProperties.GetValue(ref RibbonProperties.LabelDescription,
                        out propLabelDescription);
                var labelDescription = (string) propLabelDescription.Value;

                // get selected item pinned value
                PropVariant propPinned;
                commandExecutionProperties.GetValue(ref RibbonProperties.Pinned,
                        out propPinned);
                var pinned = (bool) propPinned.Value;

                var item = _items[(int) selectedItem];
                FireOnClick(item);
            }
            return base.Execute(verb, key, currentValue, commandExecutionProperties);
        }
    }
}